/**
 * Copyright (c) 2002-2012 旭鸣信息
 * All rights reserved. 
 * File：FrontArticlesDaoImpl.java
 * History: Dec 27, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IFrontArticlesDao;
import com.xteam.ecsystem.entities.BaseArticleGroup;
import com.xteam.ecsystem.entities.BaseArticleInfo;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 前端首页-站内快讯数据访问实现类
 * @author liuzhi
 *
 */
@Repository
public class FrontArticlesDaoImpl extends DDLPrefix implements IFrontArticlesDao {
	
	@SuppressWarnings("unchecked")
	@Autowired
	private BaseDaoImpl baseDao;

	public BaseArticleInfo getArticleInfo(String baiId) {
		try {
			String hql = SQL_FROM + BaseArticleInfo.REF + SQL_WHERE + "baiId = ?";
			
			return (BaseArticleInfo)this.baseDao.findObject(hql, new Object[]{baiId});
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@SuppressWarnings("unchecked")
	public PageCenterStation<?> getArticleList(String baiGroup, String baiTitle, int pageIndex, int pageSize) {
		try {
			List list = new ArrayList();
			
			String sql = "SELECT bai.bai_id AS baiId, bai.bai_title AS baiTitle, bai.bai_group AS " 
					+ "baiGroup, bai.bai_author AS baiAuthor, bai.bai_detail AS baiDetail, bai.bai_order " 
					+ "AS baiOrder, bai.bai_attachname AS baiAttachname, bai.bai_attachurl AS " 
					+ "baiAttachurl, bai.bai_image AS baiImage, bai.bai_time AS baiTime, bai.bai_state " 
					+ "AS baiState, bag.bag_name as bagName " 
					+ "FROM base_article_info AS bai INNER JOIN base_article_group AS bag " 
					+ "on bai.bai_group = bag.bag_id WHERE bai.bai_state = 1 AND bag.bag_state = 1 ";
					
			
			String countSql = "SELECT COUNT(*) FROM base_article_info AS bai INNER JOIN base_article_group " 
					+ "AS bag on bai.bai_group = bag.bag_id WHERE bai.bai_state = 1 AND bag.bag_state = 1";
			
			
			if(StringUtil.isNotNullOrEmpty(baiGroup)){
				sql += SQL_AND + "bai.bai_group = ?";
				countSql += SQL_AND + "bai.bai_group = ?";
				list.add(baiGroup);
			}
			
			if(StringUtil.isNotNullOrEmpty(baiTitle)){
				sql += SQL_AND + "bai.bai_title like ?";
				countSql += SQL_AND + "bai.bai_title like ?";
				list.add("%"+ baiTitle +"%");
			}
			
			if (true) {
				sql += "ORDER BY bai.bai_time DESC";
			}
			
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.
					queryWithSQL(BaseArticleInfo.class, countSql, sql, null,
							list.toArray(), (pageIndex - 1) * pageSize, pageSize);
			
			return p;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public List<?> getRecentlyArticles() {
		try {
			String hql = SQL_SELECT + " new " + BaseArticleInfo.REF + " (bai.baiId, bai.baiTitle, " 
					+ "bai.baiGroup, bai.baiAuthor, bai.baiOrder, bai.baiDetail, bai.baiAttachname, " 
					+ "bai.baiAttachurl, bai.baiImage, bai.baiTime, bai.baiState, bag.bagName) " 
					+ SQL_FROM + BaseArticleInfo.REF + " as bai, " + BaseArticleGroup.REF + " as bag "  
					+ SQL_WHERE + " bai.baiGroup = bag.bagId " + SQL_AND + "bai.baiState = 1 "
					+ SQL_AND + "bag.bagState = 1" + SQL_ORDERBY + "bai.baiTime" + SQL_DESC;
			
			return this.baseDao.find(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public List<?> getArticlesGroup(){
		try {
			String hql = SQL_FROM + BaseArticleGroup.REF + SQL_WHERE + "bagState = 1" + SQL_ORDERBY + "bagOrder";
			
			return this.baseDao.find(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public BaseArticleInfo getPreArticleInfo(String baiId, String baiGroup, int baiOrder){
		try {
			String hql = SQL_FROM + BaseArticleInfo.REF + SQL_WHERE + "baiState = 1" + SQL_AND
					+ "baiId != ?" + SQL_AND + "baiGroup = ?" + SQL_AND + "baiOrder < ?" 
					+ SQL_ORDERBY + "baiOrder" + SQL_DESC;
			
			List articles = this.baseDao.find(hql, new Object[]{baiId, baiGroup, baiOrder});
			
			if (articles != null && !articles.isEmpty()) {
				// 从降序之后的结果集中取出第一个信息实体
				return (BaseArticleInfo)articles.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public BaseArticleInfo getNextArticleInfo(String baiId, String baiGroup, int baiOrder){
		try {
			String hql = SQL_FROM + BaseArticleInfo.REF + SQL_WHERE + "baiState = 1" + SQL_AND
					+ "baiId != ?" + SQL_AND + "baiGroup = ?" + SQL_AND + "baiOrder > ?" 
					+ SQL_ORDERBY + "baiOrder";
			
			List articles = this.baseDao.find(hql, new Object[]{baiId, baiGroup, baiOrder});
			
			if (articles != null && !articles.isEmpty()) {
				// 从升序之后的结果集中取出第一个信息实体
				return (BaseArticleInfo)articles.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public BaseArticleGroup getArticleGroup(String bagId){
		try {
			String hql = SQL_FROM + BaseArticleGroup.REF + SQL_WHERE + "bagId = ?";
			
			return (BaseArticleGroup)this.baseDao.findObject(hql, new Object[]{bagId});
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
